<?php
declare(strict_types=1);

namespace App\Admin\Mapper\System;

use App\Admin\Model\System\SystemLoginLog;
use App\Admin\Model\System\SystemUploadfile;
use Illuminate\Database\Eloquent\Builder;
use Newbie\Abstracts\AbstractMapper;


/**
 * Class SystemUserMapper
 * @package App\System\Mapper
 */
class SystemUploadFileMapper extends AbstractMapper {
    /**
     * @var SystemLoginLog
     */
    public $model;


    protected $container;


    public function assignModel () {
        $this->model     = SystemUploadfile::class;
        $this->container = container();
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch (Builder $query, array $params): Builder {
        if (isset($params['storage_mode'])) {
            $query->where('storage_mode', $params['storage_mode']);
        }
        if (isset($params['origin_name'])) {
            $query->where('origin_name', 'like', '%' . $params['origin_name'] . '%');
        }
        if (isset($params['storage_path'])) {
            $query->where('storage_path', 'like', $params['storage_path'] . '%');
        }
        if (!empty($params['mime_type'])) {
            $query->where('mime_type', 'like', $params['mime_type'] . '/%');
        }
        if (isset($params['minDate']) && isset($params['maxDate'])) {
            $query->whereBetween(
                'created_at',
                [$params['minDate'] . ' 00:00:00', $params['maxDate'] . ' 23:59:59']
            );
        }
        return $query;
    }

    public function realDelete (array $ids): bool {
        foreach ($ids as $id) {
            $model = $this->model::withTrashed()->find($id);
            if ($model) {
                env('RealDeleteUploadFile', $model);
            }
        }
        unset($event);
        return true;
    }

    /**
     * 检查数据库中是否存在该目录数据
     * @param string $path
     * @return bool
     */
    public function checkDirDbExists (string $path): bool {
        return $this->model::withTrashed()
            ->where('storage_path', $path)
            ->orWhere('storage_path', 'like', $path . '/%')
            ->exists();
    }
}